# -*- coding: utf8 -*-
import os
import sys
import datetime
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import webapp
from book.models import *
from book import utils
import logging

class TodayOrderPage(webapp.RequestHandler):
    def get(self):
        total_price = 0
        orders = Order.gql('WHERE date = :1', datetime.date.today()).fetch(100)
        
        order_datas = {}
        # shops
        #   + users
        #       +balance + orders
        for order in orders:
            try:
                shop = order.dish.shop
                if not order_datas.has_key(shop.name):
                    order_datas[shop.name] = {'shop' : shop, 'users' : {}, 'shop_price' : 0}
                user = order.user
                users = order_datas[shop.name]['users']
                if not users.has_key(user.name):
                    users[user.name] = {'user' : user, 'orders' : [], 'price' : 0, 'balance' : {}}
                user_data = users[user.name]
                user_data['orders'].append(order)
                user_data['price'] = user_data['price'] + order.dish.price
                order_datas[shop.name]['shop_price'] = order_datas[shop.name]['shop_price'] + order.dish.price
                total_price = total_price + order.dish.price
            except:
                pass
        
        u"""设置每个人呢的结余"""
        users = []
        for shopName,shopInfo in order_datas.items():
            for userName,userInfo in shopInfo["users"].items():                
                balances = Balance.gql('WHERE user = :1 and date = :2', userInfo["user"],datetime.date.today()).fetch(1)
                if balances:
                    logging.getLogger().debug(u"in else %s" % userName) 
                    b = balances[0]
#                    logging.getLogger().debug(u'****************************: %s,%d, %d' % (balances[0].key, balances[0].realPay, balances[0].balancePay))
                    userInfo['balance']["balanceKey"] = b.key()
                    userInfo['balance']["realPay"] = b.realPay
                    userInfo['balance']["balancePay"] = b.balancePay 
                    
                
        u"""设置当天的收钱人"""            
        manCharge = ''
        users = User.all()            
        todayChairMans = TodayChairMan.gql('WHERE date = :1', datetime.date.today()).fetch(1)
        if todayChairMans:
           for user in users:
               try:
                   if user.key() == todayChairMans[0].user.key():
                       manCharge = user.name
                       break    
               except:
                   pass           
            
        context = {
           'current_module' : 'today',
           'orders' : orders,
           'total_price' : total_price,
           'order_datas' : order_datas,
           'manCharge'   : manCharge
        }

        utils.render_to_response(self.response, "today.html", context)
